package com.frameDemo.app;

import com.frameDemo.queue.FrameQueue;
import com.frameDemo.socket.SokectDemoServer;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by TianyuanPan on 10/16/16.
 */
public class FrameServerApp {


    private static SokectDemoServer sokectDemoServer;

    private static FrameQueue frameQueue;

    private static ExecutorService executorService;

    static {

        frameQueue = FrameQueue.getFrameQueue();
        executorService = Executors.newFixedThreadPool(10);
    }


    private static void usage() {

        System.out.println("操作提示：");
        System.out.println("\t -p <port>");
    }

    public static void main(String[] args) {

        if (args.length < 2) {
            usage();
            System.exit(0);
        }

        if (!args[0].equals("-p")) {
            usage();
            System.exit(0);
        }

        int port = -1;

        try {
            port = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
            usage();
            System.exit(0);
        }

        if (!(0 < port && port < 65536)) {
            usage();
            System.exit(0);
        }

        try {
            sokectDemoServer = SokectDemoServer.getSchedulerServerSocket(InetAddress.getByName("127.0.0.1"), port, 10, frameQueue);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.exit(1);
        }

        executorService.execute(new Runnable() {
            public void run() {
                sokectDemoServer.serverStart();
            }
        });

        while (true) {
            System.out.println("=======> 输入 q： 退出程序:");
            try {
                char c = (char) System.in.read();
                if (c == 'q' || c == 'Q')
                    System.exit(0);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
